package com.lzw.breezeNovel.webApp.task;

import com.lzw.breezeNovel.model.entity.pojo.novel.ReadTime;
import com.lzw.breezeNovel.webApp.mapper.novel.ReadTimeMapper;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.task
 * @date 2025/3/6
 * @description 用户的定时任务
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class UserInfoTask {

    /**
     * 注入用户mapper
     */
    @Autowired
    private UserInfoMapper userInfoMapper;

   /**
    * 注入阅读时间的mapper
    */
   @Autowired
   private ReadTimeMapper readTimeMapper;

   /**
    * 每天凌晨执行一次
    */
    @Scheduled(cron = "0 0 0 * * ?")
    public void UpdateDayTask(){
       //查询所有的用户的阅读时间
       List<ReadTime> readTimes = readTimeMapper.selectAll();
       //遍历所有的用户的阅读时间
       readTimes.stream().map(readTime -> {
        //获取当天的阅读时间
        String dayTime = readTime.getDayTime();
        //设置用户的一个阅读时间
        readTime.setDayTime(dayTime + readTime.getMonthTime());
        //设置用户的一个年的阅读时间
        readTime.setYearTime(readTime.getYearTime() + readTime.getMonthTime());
        return readTime;
       }).filter(readTime -> readTime != null).collect(Collectors.toList());
       //更新用户的阅读时间
       readTimeMapper.updateBatch(readTimes);
    }




}
